from PyQt6.QtWidgets import QGroupBox, QLabel,QDialog,QVBoxLayout,QPushButton,QMessageBox
from PyQt6.QtWidgets import QLineEdit,QHBoxLayout,QComboBox
from PyQt6.QtCore import QSize,Qt
from PyQt6.QtGui import QIntValidator
from db.DbmsDao import DbmsDao

class Ui_Dialog(QDialog):

    def __init__(self,parent=None,c_id=None,callback=None):
        super(Ui_Dialog,self).__init__(parent)
        self.callback_update_ui=callback
        self.c_id=c_id
        self.dbms_list=None

        self.setupUi()
        

    def setupUi(self):
        if self.c_id:
            self.setWindowTitle("修改数据库实例")
            self.dbms_list=DbmsDao.get_db_info_by_id(self.c_id)
        else:
            self.setWindowTitle("添加数据库实例")
        self.resize(500, 400)

        layout=QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        groupbox= QGroupBox()
        groupbox.setMaximumWidth(480)
        groupbox.setTitle('基本数据')

        layout_dbms = QVBoxLayout()
        layout_dbms.setAlignment(Qt.AlignmentFlag.AlignTop)
          
        layout_name = QHBoxLayout()
        layout_name.setAlignment(Qt.AlignmentFlag.AlignLeft)
        label=QLabel('* 名    称')
        label.setFixedSize(QSize(60,30))
        layout_name.addWidget(label)
        self.edit_name=QLineEdit()
        self.edit_name.setPlaceholderText('输入名称')
        self.edit_name.setMaxLength(32)
        layout_name.addWidget(self.edit_name)
        layout_dbms.addLayout(layout_name)

        layout_type = QHBoxLayout()
        layout_type.setAlignment(Qt.AlignmentFlag.AlignLeft)
        label=QLabel('选择类型')
        label.setFixedSize(QSize(60,30))
        layout_type.addWidget(label)
        self.combobox_type = QComboBox()
        self.combobox_type.setMaximumWidth(300)
        self.combobox_type.addItem('mysql')
        self.combobox_type.addItem('sqlite')
        layout_type.addWidget(self.combobox_type)
        layout_dbms.addLayout(layout_type)

        layout_host = QHBoxLayout()
        layout_host.setAlignment(Qt.AlignmentFlag.AlignLeft)
        label=QLabel('* host')
        label.setFixedSize(QSize(60,30))
        layout_host.addWidget(label)
        self.edit_host=QLineEdit('localhost')
        self.edit_host.setFixedSize(QSize(100,30))
        self.edit_host.setMaxLength(32)
        layout_host.addWidget(self.edit_host)

        label=QLabel(' : ')
        label.setFixedSize(QSize(60,30))
        layout_host.addWidget(label)
        self.edit_port=QLineEdit('')
        self.edit_port.setFixedSize(QSize(60,30))
        #self.edit_port.setValidator(QIntValidator())
        self.edit_port.setMaxLength(5)
        layout_host.addWidget(self.edit_port)
        layout_dbms.addLayout(layout_host)


        layout_username = QHBoxLayout()
        layout_username.setAlignment(Qt.AlignmentFlag.AlignLeft)
        label=QLabel('* 用户名')
        label.setFixedSize(QSize(60,30))
        layout_username.addWidget(label)
        self.edit_username=QLineEdit('')
        self.edit_username.setMaxLength(32)
        layout_username.addWidget(self.edit_username)
        layout_dbms.addLayout(layout_username)

        layout_password = QHBoxLayout()
        layout_password.setAlignment(Qt.AlignmentFlag.AlignLeft)
        label=QLabel('* 密    码')
        label.setFixedSize(QSize(60,30))
        layout_password.addWidget(label)
        self.edit_password=QLineEdit('')
        self.edit_password.setMaxLength(32)
        layout_password.addWidget(self.edit_password)
        layout_dbms.addLayout(layout_password)

        layout_remark = QHBoxLayout()
        layout_remark.setAlignment(Qt.AlignmentFlag.AlignLeft)
        label=QLabel('备    注')
        label.setFixedSize(QSize(60,30))
        layout_remark.addWidget(label)
        self.edit_remark=QLineEdit('')
        self.edit_remark.setMaxLength(32)
        layout_remark.addWidget(self.edit_remark)
        layout_dbms.addLayout(layout_remark)

        groupbox.setLayout(layout_dbms)

        layout.addWidget(groupbox)

        #底部按钮
        layout_btn=QHBoxLayout()
        layout_btn.setAlignment(Qt.AlignmentFlag.AlignHCenter)

        btn_save=QPushButton('保存')
        btn_save.setFixedSize(QSize(150,30))
        layout_btn.addWidget(btn_save)
        btn_save.clicked.connect(self.on_clicked_btn_save)

        btn_cancel=QPushButton('取消')
        btn_cancel.setFixedSize(QSize(150,30))
        layout_btn.addWidget(btn_cancel)
        btn_cancel.clicked.connect(self.on_clicked_btn_cancel)
        
        layout.addLayout(layout_btn)

        self.init_data()

        self.setLayout(layout)

    def init_data(self):
        if self.dbms_list and len(self.dbms_list)>0:
            self.edit_name.setText(self.dbms_list[0][1])
            self.combobox_type.setCurrentText(self.dbms_list[0][2])
            self.edit_host.setText(self.dbms_list[0][3])
            self.edit_port.setText(str(self.dbms_list[0][4]))
            self.edit_username.setText(self.dbms_list[0][5])
            self.edit_password.setText(self.dbms_list[0][6])
            self.edit_remark.setText(self.dbms_list[0][7])


    def on_clicked_btn_save(self):
        name=self.edit_name.text()
        type=self.combobox_type.currentText()
        host=self.edit_host.text()
        port=self.edit_port.text()
        user_name=self.edit_username.text()
        password=self.edit_password.text()
        remark=self.edit_remark.text()
        
        if self.c_id:
            
            result= DbmsDao.update_db_info(self.c_id,name,type,host,port,user_name,password,remark)
            if result == False:
                QMessageBox.warning(self,'错误提示','数据更新失败')
        else :
            result= DbmsDao.insert_db_info(name,type,host,port,user_name,password,remark)
            if result == False:
                QMessageBox.warning(self,'错误提示','数据插入失败')

        if self.callback_update_ui:
            self.callback_update_ui()

        self.close()

    def on_clicked_btn_cancel(self):
        self.close()
